﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Linq;
using ScrumTable.BL.DM.Properties;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Schema;
using System.Collections.Generic;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// This class contains mappings between scrum item states and their
    /// names.
    /// </summary>
    public class ScrumStateMapper : IEnumerable<ScrumState>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Dictionary<string, ScrumState> _mappings  = new Dictionary<string, ScrumState>();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the number of states stored in the current instance.
        /// </summary>
        public int Count
        {
            get { return _mappings.Count; }
        }

        /// <summary>
        /// Gets the ScrumState with the given name.
        /// </summary>
        /// <param name="name">Specifies the name of the state to retrieve.</param>
        /// <returns>Returns the element or throws an exception if it could not be found.</returns>
        public ScrumState this[string name]
        {
            get
            {
                if (!_mappings.ContainsKey(name))
                {
                    throw new ScrumStateException(name);
                }
                return _mappings[name];
            }
        }
        
        /// <summary>
        /// Gets the ScrumState with the given id.
        /// </summary>
        /// <param name="id">Specifies the id of the state to retrieve.</param>
        /// <returns>Returns the element or throws an exception if it could not be found.</returns>
        public ScrumState this[int id]
        {
            get { return _mappings.Values.First(state => state.Id == id); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        internal ScrumStateMapper()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if the current state mapper contains the given ScrumState.
        /// </summary>
        /// <param name="state">Specifies the state of the mapper to check.</param>
        public bool Contains(ScrumState state)
        {
            return (state != null && _mappings.ContainsValue(state));
        }

        /// <summary>
        /// Returns true if the current state mapper contains the given ScrumState.
        /// </summary>
        /// <param name="state">Specifies the state of the mapper to check.</param>
        public bool ContainsName(string state)
        {
            return (state != null && _mappings.ContainsKey(state));
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<ScrumState> GetEnumerator()
        {
            return (from state in _mappings.Values
                    orderby state.Id
                    select state).GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        internal void TryAddState(string stateName)
        {
            PreCondition.AssertNotNull(stateName, "StateName");

            if (!_mappings.ContainsKey(stateName))
            {
                _mappings[stateName] = new ScrumState(stateName, _mappings.Values.Count);
            }
        }

        #endregion
    }
}